package subject.impl;

import observer.Investor;
import subject.StockMarket;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体主题：股票
 * @author Liu Yicong
 * @date 2025/9/23
 */
public class Stock implements StockMarket {
	private String stockName;
	private double price;
	private List<Investor> investors = new ArrayList<>();

	public Stock(String name, double price) {
		this.stockName = name;
		this.price = price;
	}

	@Override
	public void register(Investor investor) {
		investors.add(investor);
		System.out.println(investor.getClass().getSimpleName() + " 已订阅股票: " + stockName);
	}

	@Override
	public void remove(Investor investor) {
		investors.remove(investor);
		System.out.println(investor.getClass().getSimpleName() + " 已取消订阅股票: " + stockName);
	}

	@Override
	public void notifyInvestors() {
		System.out.println("---------------------------------");
		System.out.println("股票 [" + stockName + "] 价格变动，正在通知所有投资者...");
		for (Investor investor : investors) {
			investor.update(stockName, price);
		}
	}

	// 改变股价的方法，触发通知
	public void setPrice(double newPrice) {
		this.price = newPrice;
		notifyInvestors();
	}
}
